<?php

namespace app\controller\admin;

use think\annotation\route\Route;
use think\facade\Request;
use app\common\model\Admin;
use think\annotation\route\Get;
use think\annotation\route\Post;
use think\annotation\route\Group;
use app\common\model\Config;

/**
 * 配置 控制器类
 */
#[Group('admin/config')]
class ConfigController extends AdminAuthorizedController
{
    /**
     * 保存配置
     * @param Admin $admin
     * @return Config|array|mixed|\think\Model
     * @throws \app\common\exception\CatchException
     */
    #[Post('setConfigByCode')]
    public function setConfig(Admin $admin)
    {
        [
            'code' => $code,
            'content' => $content
        ] = $param = Request::post();

        //权限判断
        $this->checkPower($admin, 'Config' . '_' . $code);

        $config = (new Config())->where('code', '=', $code)->findOrEmpty();
        $config->code = $code;
        $config->content = $content;
        $config->save();

        return $config;
    }

    /**
     * 获取配置
     * @param Admin $admin
     * @return \stdClass
     * @throws \app\common\exception\CatchException
     */
    #[Route('GET|POST', 'getByCode')]
    public function getConfigByCode(Admin $admin)
    {
        $code = trim(input('code',''));

        //权限判断
        $this->checkPower($admin, 'Config' . '_' . $code);

        return Config::getConfigContent($code);
    }
}
